package com.hy.admin.domain.shared.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.hy.admin.domain.user.Privilege;
import com.hy.admin.domain.user.User;
import com.hy.admin.entity.MemberEntity;

import freemarker.core.ReturnInstruction.Return;


public class HyUserDetails implements UserDetails {

	private static final long serialVersionUID = -279971901222864319L;
	
	private String username;
    private String password;
    private String notpassword;
    private List<Privilege> privileges = new ArrayList<>();

	protected MemberEntity memberEntity;
	
	/**
     * 用户的授权集合
     */
    protected List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
    
    /**
     * 角色权限 前缀
     *
     * @see org.springframework.security.access.vote.RoleVoter
     */
    protected static final String ROLE_PREFIX = "ROLE_";
    
    /**
     * 默认的 用户角色
     * ROLE_USER
     */
    protected static final GrantedAuthority DEFAULT_USER_ROLE = new SimpleGrantedAuthority(ROLE_PREFIX + Privilege.USER.name());
	
	public HyUserDetails(GrantedAuthority authority) {
//		this.memberEntity=memberEntity;
		this.initialAuthorities(authority);
	}
	
	/**
     * 初始化用户角色,权限
     */
    private void initialAuthorities(GrantedAuthority authority) {
        //Default, everyone have it
        this.grantedAuthorities.add(DEFAULT_USER_ROLE);
        this.grantedAuthorities.add(authority);
        //default user have all privileges
//        this.grantedAuthorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + Privilege.UNITY.name()));
//        this.grantedAuthorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + Privilege.MOBILE.name()));
    }
	
    
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return this.grantedAuthorities;
	}

	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public String getUsername() {
		return this.username;
	}

	/* 账户是否未过期 */
	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	/*账户是否未锁定 */
	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	/* 密码是否未过期 */
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	/*账户是否启用,默认true (启用)*/
	@Override
	public boolean isEnabled() {
		return true;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public List<Privilege> privileges() {
        return privileges;
    }

	public MemberEntity getMemberEntity() {
		return memberEntity;
	}

	public void setMemberEntity(MemberEntity memberEntity) {
		this.memberEntity = memberEntity;
	}

	public String getNotpassword() {
		return notpassword;
	}

	public void setNotpassword(String notpassword) {
		this.notpassword = notpassword;
	}

	
	

}
